<template>
  <el-dialog class="pick-dialog" title="特殊操作" :visible.sync="dialogVisible" @close="close" :close-on-click-modal="false" width="600px">
    <div class="form-box" v-loading="loading">
      <el-form ref="ruleForm" :model="ruleForm" label-width="auto">
        <el-form-item label="创建人" prop="createId">
          <el-select :disabled="current!='createId'" filterable clearable v-model="ruleForm.createId" placeholder="请选择回收人">
            <el-option v-for="(item ,key) in yuangongList" :key="key" :label="item.nickName" :value="item.userId" />
          </el-select>
          <span style="margin-left: 15px;">
            <el-button v-if="current!='createId'" type="primary" @click="editStart('createId')">编辑</el-button>
            <el-button v-if="current=='createId'" type="success" @click="save('createId')">完成</el-button>
            <el-button v-if="current=='createId'" type="primary" plain @click="cancel('createId')">取消</el-button>
          </span>

        </el-form-item>

        <div class="center">
          <el-button @click="close">关闭</el-button>
          <!-- <el-button type="primary" @click="submitForm">提交</el-button> -->
        </div>
      </el-form>
    </div>
  </el-dialog>
</template>

<script>
import supplyConfigApi from "@/api/system/supplyConfig";
import goodsListApi from "../../../api/gameConfig/goodsList";
export default {
  name: "pick",
  data() {
    return {
      dialogVisible: false,
      ruleForm: {
        createId: null,
        id: "",
      },
      initForm: {},
      yuangongList: [],
      loading: false,
      current: "",
    };
  },
  created() {},
  methods: {
    open(info) {
      this.current = ''
      this.initForm = JSON.parse(JSON.stringify(info));
      // this.ruleForm.createName = info.createName;
      this.ruleForm.createId = info.createId;
      this.ruleForm.id = info.id;
      this.dialogVisible = true;
      this.getYuangongList();
    },
    submitForm() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          this.loading = true;
        } else {
          this.$alert("请填写完整！");
          return false;
        }
      });
    },
    //点击编辑
    editStart(e) {
      if (this.current) {
        // 恢复初始值
        this.ruleForm[this.current] = this.initForm[this.current];
      }
      this.current = e || "";
    },
    save(e) {
      // 调用后端保存方法
      const obj = {}
      if(e=='createId'){
        obj.teshuType = 1
        obj.id = this.ruleForm.id
        obj.createId = this.ruleForm.createId
      }
      goodsListApi.goodsTeshu(obj).then(res=>{
        console.log("%c Line:82 🍫 res", "color:#fca650", res);
        if(res.code==200){
          this.$message.success(res.msg)
        }
      })
      // 成功后关闭编辑状态
      this.current = "";
    },
    cancel(e) {
      if (this.current) {
        // 恢复初始值
        this.ruleForm[this.current] = this.initForm[this.current];
      }
      this.current = "";
    },
    // 获取所有员工列表
    getYuangongList() {
      supplyConfigApi
        .getAllUserList({
          userNewType: 1,
        })
        .then((res) => {
          this.yuangongList = res.rows;
        });
    },
    // // 员工选择改变
    // createIdChange(e){
    //   if (e) {
    //     for (let i = 0; i < this.yuangongList.length; i++) {
    //       if (this.yuangongList[i].userId == e) {
    //         this.ruleForm.createName = this.yuangongList[i].nickName;
    //       }
    //     }
    //   } else {
    //     this.ruleForm.createName = "";
    //   }
    // },
    close() {
      this.resetForm();
      this.dialogVisible = false;
      this.$emit('close')
    },
    resetForm() {
      this.$refs["ruleForm"].resetFields();
    },
  },
};
</script>
<style scoped lang="css">
</style>
